<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-9-22
 * Time: 17:54
 */
Yaf_Loader::import(CONTROLLERS_PATH.'Controller.php');

class AboutController extends Controller
{
    public function init()
    {
        $this->authentication = $this->authentication();
        if(!$this->authentication)
        {
            $this->redirect('/');
            exit;
        }
        $this->model = new ModelModel();
    }

    /**
     * 图片上传
     */
    public function uploadAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $file = $_FILES['img'];
            $type =pathinfo($file["name"]);
            if($file['size'] > 2097152){
                $code = 400;
                $data['message'] = '图片尺寸不可超过2M';
            }elseif($type['extension'] !=  'png' && $type['extension'] !=  'jpg' && $type['extension'] !=  'jpeg' && $type['extension'] !=  'PNG' && $type['extension'] !=  'JPG' && $type['extension'] !=  'JPEG'){
                $code = 400;
                $data['message'] = '只能上传PNG和JPG格式的图片';
            }elseif($file['error']){
                $code = 400;
                $data['message'] = '图片上传失败、请联系管理员';
            }else{
                $data = $this->model->uploadAction($file);
                if(!empty($data['code'])){
                    $code = 400;
                }else{
                    $code = 200;
                }
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }
}